package team.scau.laboratory_repair.web.pc;

import lombok.extern.apachecommons.CommonsLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import team.scau.laboratory_repair.common.base.BaseController;
import team.scau.laboratory_repair.modules.analysis.bargraph.*;
import team.scau.laboratory_repair.modules.analysis.piechart.*;
import team.scau.laboratory_repair.service.RecordService;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description:
 * @Author:ZTF
 * @Date:2018/10/29 16:18
 */
@Controller
@CommonsLog
@RequestMapping(value="/pc/biz/registrantAlysis")
public class RegistrantAnalysisController extends BaseController {
    @Autowired
    private RecordService recordService;

    @RequiresPermissions("biz:registrantAlysis:list")
    @GetMapping(value = "/list", produces = "text/html;charset=utf-8")
    public String list(Model model) throws Exception{
        return "/pc/biz/registrantAlysis/list";
    }

    @ResponseBody
    @RequiresPermissions("biz:registrantAlysis:list")
    @RequestMapping(value = "/bargraph.json", produces = "application/json;charset=utf-8")
    public BarGraphDto bargraphList() throws Exception {
        BarGraphDto barGraphDto = new BarGraphDto();
        BarTitleBean title=new BarTitleBean();
        BarTooltipBean tooltip=new BarTooltipBean();
        BarLegendBean legend=new BarLegendBean();
        BarXAxisBean xAxis=new BarXAxisBean();
        BarYAxisBean yAxis=new BarYAxisBean();
        List<BarSeriesBean> seriesBeanList=new ArrayList<>();
        BarSeriesBean series=new BarSeriesBean();

        title.setText("报修角色类型分析");
        List<String> legendList=new ArrayList<>();
        legendList.add("该角色数量");
        legend.setData(legendList);
        List<Object[]> objectsList = recordService.listRegistrantCount();
        List<BigInteger> xAxisList = new ArrayList<>();
        List<String> seriesList = new ArrayList<>();
        for(Object[] objects:objectsList){
            xAxisList.add((BigInteger)objects[0]);
            seriesList.add((String)objects[1]);
        }
        xAxis.setData(seriesList);
        series.setName("数量");
        series.setType("bar");



        series.setData(xAxisList);


        seriesBeanList.add(series);
        barGraphDto.setTitle(title);
        barGraphDto.setTooltip(tooltip);
        barGraphDto.setLegend(legend);
        barGraphDto.setXAxis(xAxis);
        barGraphDto.setYAxis(yAxis);
        barGraphDto.setSeries(seriesBeanList);
        return barGraphDto;
    }


    @ResponseBody
    @RequiresPermissions("biz:registrantAlysis:list")
    @RequestMapping(value = "/piechart.json", produces = "application/json;charset=utf-8")
    public PieChartDto piechartList() throws Exception {
        PieChartDto pieChartDto=new PieChartDto();

        PieTitleBean pieTitleBean=new PieTitleBean();
        pieTitleBean.setText("报修角色类型分析");
        pieTitleBean.setSubtext("华南农业大学");
        pieTitleBean.setX("center");

        List<Object[]> objectsList = recordService.listRegistrantCount();
        List<String > nameList = new ArrayList<>();
        List<PieDataBean> sumList = new ArrayList<>();
        for(Object[] objects:objectsList){
            nameList.add((String) objects[1]);
            sumList.add(PieDataBean.builder().value((BigInteger)objects[0]).name((String)objects[1]).build());
        }

        PieTooltipBean pieTooltipBean=new PieTooltipBean();
        pieTooltipBean.setFormatter("item");
        pieTooltipBean.setTrigger("{a} <br/>{b} : {c} ({d}%)");

        PieLegendBean pieLegendBean=new PieLegendBean();
        pieLegendBean.setOrient("vertical");
        pieLegendBean.setLeft("left");
        pieLegendBean.setData(nameList);

        List<PieSeriesBean> seriesList=new ArrayList<>();
        PieSeriesBean pieSeriesBean=new PieSeriesBean();
        pieSeriesBean.setName("角色类型");
        pieSeriesBean.setType("pie");
        pieSeriesBean.setRadius("55%");
        List<String> centerList=new ArrayList<>();
        centerList.add("50%");
        centerList.add("60%");
        pieSeriesBean.setCenter(centerList);


        PieItemStyleBean pieItemStyleBean=new PieItemStyleBean();

        PieEmphasisBean pieEmphasisBean=new PieEmphasisBean();
        pieEmphasisBean.setShadowBlur(10);
        pieEmphasisBean.setShadowColor("rgba(0, 0, 0, 0.5)");
        pieEmphasisBean.setShadowOffsetX(0);
        pieItemStyleBean.setEmphasis(pieEmphasisBean);

        pieSeriesBean.setData(sumList);
        pieSeriesBean.setItemStyle(pieItemStyleBean);


        seriesList.add(pieSeriesBean);
        pieChartDto.setTooltip(pieTooltipBean);
        pieChartDto.setTitle(pieTitleBean);
        pieChartDto.setLegend(pieLegendBean);
        pieChartDto.setSeries(seriesList);
        return pieChartDto;
    }


}
